<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;
use Think\Exception;

class AdviceController extends AdminbaseController{
	
	public function _initialize() {
		parent::_initialize();
	}

    //用户建议
    public function user(){
        $where['a.type']=2;
        $model=M('Advice');
        $count=$model
            ->alias('a')
            ->where($where)
            ->count();
        $page = $this->page($count, 20);
        $advices=$model
            ->alias('a')
            ->join('LEFT JOIN __USER__ as u ON u.id=a.user_id')
            ->where($where)
            ->field('u.username,u.phone,a.id')
            ->order("a.create_time DESC")
            ->limit($page->firstRow, $page->listRows)
            ->select();
        $this->assign('advices',$advices);
        $this->assign("page", $page->show('Admin'));
        $this->display();
    }

    //获得建议详情
    public function getDetails(){
        $id=I('id',0,'intval');
        if(!$id){
            $this->error('数据错误');
        }
        $advice=M('Advice')->where(['id'=>$id])->find();
        $this->assign('advice',$advice);
        $this->display('getDetails');
    }

    //商户建议
    public function business(){
        $where['a.type']=1;
        $model=M('Advice');
        $count=$model->alias('a')->where($where)->count();
        $page = $this->page($count, 20);
        $advices=$model
            ->alias('a')
            ->join('LEFT JOIN __BUSINESS__ as b ON b.id=a.user_id')
            ->where($where)
            ->field('b.name,b.phone,a.id')
            ->order("a.create_time DESC")
            ->limit($page->firstRow, $page->listRows)
            ->select();
        $this->assign('advices',$advices);
        $this->assign("page", $page->show('Admin'));
        $this->display();
    }

    //建议删除
    public function delete(){
        $id=I('id',0,'intval');
        if(!$id){
            $this->error('数据错误');
        }
        $advice=M('Advice')->where(['id'=>$id])->find();
        if($advice){
            $result=M('Advice')->where(['id'=>$id])->delete();
            if($result!==false){
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }else{
            $this->error('数据不存在');
        }
    }

    //服务订单投诉
    public function order(){
        $where['o.is_complained']=1;
        $where['o.is_solved']=0;
        $model=M('Order');
        $count=$model->alias('o')->where($where)->count();
        $page = $this->page($count, 20);
        $orders=$model
            ->alias('o')
            ->join('LEFT JOIN __BUSINESS_SERVICE__ as bs ON bs.id=o.business_service_id')
            ->join('LEFT JOIN __BUSINESS__ as b ON b.id=bs.business_id')
            ->join('LEFT JOIN __USER__ as u ON u.id=o.user_id')
            ->where($where)
            ->field('b.name,u.username,u.phone,o.business_service_name,o.id,o.sn,o.total')
            ->order("o.create_time DESC")
            ->limit($page->firstRow, $page->listRows)
            ->select();
        $this->assign('orders',$orders);
        $this->assign("page", $page->show('Admin'));
        $this->display();
    }

    //获取服务订单投诉详情
    public function orderDetails(){
        $id=I('id',0,'intval');
        if(!$id){
            $this->error('数据错误');
        }
        $order=M('Order')->where(['id'=>$id,'is_complained'=>1,'is_solved'=>0])->find();
        if($order){
            $order['complain_images']=empty($order['complain_images'])?'':json_decode($order['complain_images']);
            $this->assign('order',$order);
            $this->display('orderDetails');
        }else{
            $this->error('订单不存在');
        }
    }

    //服务订单投诉解决
    public function orderSolved(){
        $id=I('id',0,'intval');
        if(!$id){
            $this->error('数据错误');
        }
        $order=M('Order')->where(['id'=>$id,'is_complained'=>1,'is_solved'=>0])->find();
        if($order){
            $result=M('Order')->where(['id'=>$id])->save(['is_solved'=>1]);
            if($result!==false){
                $this->success('解决成功');
            }else{
                $this->error('解决失败');
            }
        }else{
            $this->error('订单不存在');
        }
    }


    //活动订单投诉
    public function orderActivity(){
        $where['o.is_complained']=1;
        $where['o.is_solved']=0;
        $model=M('OrderActivity');
        $count=$model->alias('o')->where($where)->count();
        $page = $this->page($count, 20);
        $orders=$model
            ->alias('o')
            ->join('LEFT JOIN __BUSINESS_ACTIVITY__ as ba ON ba.id=o.business_activity_id')
            ->join('LEFT JOIN __BUSINESS__ as b ON b.id=ba.business_id')
            ->join('LEFT JOIN __USER__ as u ON u.id=o.user_id')
            ->where($where)
            ->field('b.name,u.username,u.phone,ba.name as business_activity_name,o.id,o.sn,o.total,ba.start_time,ba.end_time')
            ->order("o.create_time DESC")
            ->limit($page->firstRow, $page->listRows)
            ->select();
        $this->assign('orders',$orders);
        $this->assign("page", $page->show('Admin'));
        $this->display();
    }

    //获取活动订单投诉详情
    public function orderActivityDetails(){
        $id=I('id',0,'intval');
        if(!$id){
            $this->error('数据错误');
        }
        $order=M('OrderActivity')->where(['id'=>$id,'is_complained'=>1,'is_solved'=>0])->find();
        if($order){
            $order['complain_images']=empty($order['complain_images'])?'':json_decode($order['complain_images']);
            $this->assign('order',$order);
            $this->display('orderActivityDetails');
        }else{
            $this->error('订单不存在');
        }
    }

    //活动订单投诉解决
    public function orderActivitySolved(){
        $id=I('id',0,'intval');
        if(!$id){
            $this->error('数据错误');
        }
        $order=M('OrderActivity')->where(['id'=>$id,'is_complained'=>1,'is_solved'=>0])->find();
        if($order){
            $result=M('OrderActivity')->where(['id'=>$id])->save(['is_solved'=>1]);
            if($result!==false){
                $this->success('解决成功');
            }else{
                $this->error('解决失败');
            }
        }else{
            $this->error('订单不存在');
        }
    }
}